﻿using System;
using System.Collections.Generic;
using System.Text;

using System.Net;
using System.IO;

namespace gTray
{
    public class NetService
    {
        public const string GoogleLoginAddress = "https://www.google.com/accounts/ClientLogin";
        /// <summary>
        /// Checks the credentials via HTTP.
        /// </summary>
        /// <param name="credential">The credentials to check.</param>
        /// <returns></returns>
        public static HttpStatusCode CheckCredentialsHTTP(NetworkCredential credential)
        {
            /* Code from the MSDN page
             * http://msdn.microsoft.com/en-us/library/debx8sh9.aspx
             * 
             * Information for the Google Login Authentication is here
             * http://code.google.com/apis/accounts/docs/AuthForInstalledApps.html
             */

            string post_data = "accountType=HOSTED_OR_GOOGLE&Email="+credential.UserName + "@gmail.com&Passwd="+credential.Password + "&service=xapi&source=APitts-gTray-1.0";

            // Create a request using a URL that can receive a post. 
            WebRequest request = WebRequest.Create(GoogleLoginAddress);

            // Set the Method property of the request to POST.
            request.Method = "POST";

            byte[] byteArray = Encoding.UTF8.GetBytes(post_data);

            // Set the ContentType property of the WebRequest.
            request.ContentType = "application/x-www-form-urlencoded";

            // Set the ContentLength property of the WebRequest.
            request.ContentLength = byteArray.Length;
           
            // Get the request stream.
            Stream dataStream = request.GetRequestStream();
            
            // Write the data to the request stream.
            dataStream.Write(byteArray, 0, byteArray.Length);
            
            // Close the Stream object.
            dataStream.Close();

            // Get the response.
            HttpWebResponse response;
            try
            {
                response = (HttpWebResponse)request.GetResponse();
            }
            catch (WebException wEx) //Catches the 403 non-authorized error
            {
                return ((HttpWebResponse)wEx.Response).StatusCode;
            }

            return response.StatusCode;
        }

        /// <summary>
        /// Checks the credentials via HTTP.
        /// </summary>
        /// <param name="username">The username.</param>
        /// <param name="password">The password.</param>
        /// <returns></returns>
        public static HttpStatusCode CheckCredentialsHTTP(string username, string password)
        {
            return CheckCredentialsHTTP(new NetworkCredential(username, password));
        }

        public static bool IsConnectedToInternet()
        {
            try
            {
                var entry = System.Net.Dns.GetHostEntry("www.google.com");
                return true;
            }
            catch
            {
                return false; // host not reachable.
            } 
        }
    }
}
